<template>
    <div class="app-container">
        <el-row :gutter="20">
            <!--用户数据-->
            <el-col :span="24" :xs="24">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                    label-width="68px">
                    <el-form-item label="节点ID" prop="id">
                        <el-input v-model="queryParams.id" placeholder="请输入节点ID" clearable style="width: 240px"
                            @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="主机地址" prop="name">
                        <el-input v-model="queryParams.name" placeholder="请输入主机名" clearable style="width: 240px"
                            @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                            @click="handleUpdate">修改</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                            @click="handleDelete">删除</el-button>
                    </el-col>
                    <!-- <el-col :span="1.5">
                        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
                            v-hasPermi="['system:user:import']">导入</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="warning" plain icon="el-icon-download" size="mini"
                            @click="handleExport">导出</el-button>
                    </el-col> -->
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
                </el-row>

                <el-table v-loading="loading" :data="nodeList" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="50" align="center" />
                    <el-table-column label="节点ID" align="center" key="id" prop="id" width="100" v-if="columns[0].visible" />
                    <el-table-column label="节点名称" align="center" key="alias" prop="alias" v-if="columns[1].visible"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="主机地址" align="center" key="name" prop="name" v-if="columns[2].visible"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="端口" align="center" key="port" prop="port" v-if="columns[3].visible"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="查看任务" key="show" v-if="columns[4].visible">
                        <template slot-scope="scope">
                            <el-button type="success" @click="toTasks(scope.row)">查看任务</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" align="center" key="remark" prop="remark" v-if="columns[5].visible"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-edit"
                                @click="handleUpdate(scope.row)">修改</el-button>
                            <el-button size="mini" type="text" icon="el-icon-delete"
                                @click="handleDelete(scope.row)">删除</el-button>
                            <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)">
                                <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
                                <el-dropdown-menu slot="dropdown">
                                    <el-dropdown-item command="ping" icon="el-icon-key">测试连接</el-dropdown-item>
                                </el-dropdown-menu>
                            </el-dropdown>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page"
                    :limit.sync="queryParams.page_size" @pagination="getList" />
            </el-col>
        </el-row>

        <!-- 添加或修改用户配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="formRules" label-width="80px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="节点名称" prop="alias">
                            <el-input v-model="form.alias" placeholder="输入节点名称" auto-complete="off" maxlength="30" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="主机地址" prop="name">
                            <el-input v-model="form.name" placeholder="输入主机地址" maxlength="50" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="端口号" prop="port">
                            <el-input-number v-model.number="form.port" controls-position="right" :min="1" :max="65535" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input type="textarea" :autosize="{ minRows: 4, maxRows: 10 }" placeholder=""
                                v-model="form.remark"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import { listNode, updateNode, addNode, getNode, ping, delNode } from "@/api/monitor/task";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
    name: "User",
    dicts: [
        //   'sys_normal_disable', 
        // 'sys_user_sex'
    ],
    components: { Treeselect },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 节点表格数据
            nodeList: null,
            // 弹出层标题
            title: "",
            // 部门树选项
            deptOptions: undefined,
            // 是否显示弹出层
            open: false,
            // 部门名称
            deptName: undefined,
            // 默认密码
            initPassword: undefined,
            // 日期范围
            dateRange: [],
            // 岗位选项
            postOptions: [],
            // 角色选项
            roleOptions: [],
            // 表单参数
            form: {
                id: '',
                name: '',
                port: 5921,
                alias: '',
                remark: ''
            },
            formRules: {
                name: [
                    { required: true, message: '请输入主机名', trigger: 'blur' }
                ],
                port: [
                    { required: true, message: '请输入端口', trigger: 'blur' },
                    { type: 'number', message: '端口无效' }
                ],
                alias: [
                    { required: true, message: '请输入节点名称', trigger: 'blur' }
                ]
            },
            defaultProps: {
                children: "children",
                label: "label"
            },
            // 用户导入参数
            upload: {
                // 是否显示弹出层（用户导入）
                open: false,
                // 弹出层标题（用户导入）
                title: "",
                // 是否禁用上传
                isUploading: false,
                // 是否更新已经存在的用户数据
                updateSupport: 0,
                // 设置上传的请求头部
                headers: { Authorization: "Bearer " + getToken() },
                // 上传的地址
                url: process.env.VUE_APP_BASE_API + "/system/user/importData"
            },
            // 查询参数
            queryParams: {
                page: 1,
                page_size: 10,
                id: '',
                name: '',
                alias: ''
            },
            // 列信息
            columns: [
                { key: 0, label: `任务ID`, visible: true },
                { key: 1, label: `节点名称`, visible: true },
                { key: 2, label: `主机名称`, visible: true },
                { key: 3, label: `端口号`, visible: true },
                { key: 4, label: `查看任务`, visible: true },
                { key: 5, label: `备注`, visible: true }
            ],
            // 表单校验
            rules: {
                name: [
                    { required: true, message: "用户名称不能为空", trigger: "blur" },
                    { min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: "用户密码不能为空", trigger: "blur" },
                    { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
                ],
                confirm_password: [
                    { required: true, message: "确认密码不能为空", trigger: "blur" },
                    { validator: this.checkpassword, message: '2次输入的密码不一致', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: "请输入邮箱地址", trigger: "blur" },
                    {
                        type: "email",
                        message: "请输入正确的邮箱地址",
                        trigger: ["blur", "change"]
                    }
                ]
            }
        };
    },
    watch: {
    },
    created() {
        this.getList();
    },
    methods: {
        toTasks(row) {
            this.$router.push(
                {
                    path: '/monitor/job',
                    query: {
                        host_id: row.id
                    }
                })
        },
        ping(row) {
            ping(row.id).then(response => {
                this.$message.success('连接成功')
            })
        },
        /** 查询用户列表 */
        getList() {
            this.loading = true;
            listNode(this.queryParams).then(response => {
                this.nodeList = response.data.data;
                this.total = response.data.total;
                this.loading = false;
            }
            );
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: '',
                name: '',
                port: 5921,
                alias: '',
                remark: ''
            }
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.page = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        // 更多操作触发
        handleCommand(command, row) {
            switch (command) {
                case "ping":
                    this.ping(row);
                    break;
                default:
                    break;
            }
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "新增节点";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const nodeId = row.id || this.ids;
            getNode(nodeId).then(response => {
                console.log(response.data)
                this.form = response.data;
                this.open = true;
                this.title = "节点修改";
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (!valid) {
                    return false;
                }
                if (this.form.id != undefined) {
                    updateNode(this.form).then(response => {
                        this.$modal.msgSuccess("修改成功");
                        this.open = false;
                        this.getList();
                    });
                } else {
                    addNode(this.form).then(response => {
                        this.$modal.msgSuccess("新增成功");
                        this.open = false;
                        this.getList();
                    });
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const nodeIds = row.id || this.ids;
            this.$modal.confirm('是否确认删除节点编号为"' + nodeIds + '"的数据项？').then(function () {
                return delNode(nodeIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        // handleExport() {
        //     this.download('system/user/export', {
        //         ...this.queryParams
        //     }, `user_${new Date().getTime()}.xlsx`)
        // },
        // /** 下载模板操作 */
        // importTemplate() {
        //     this.download('system/user/importTemplate', {
        //     }, `user_template_${new Date().getTime()}.xlsx`)
        // },
        // // 文件上传成功处理
        // handleFileSuccess(response, file, fileList) {
        //     this.upload.open = false;
        //     this.upload.isUploading = false;
        //     this.$refs.upload.clearFiles();
        //     this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
        //     this.getList();
        // },
        // // 提交上传文件
        // submitFileForm() {
        //     this.$refs.upload.submit();
        // }
    }
};
</script>

<style lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
    text-align: left;
}
</style>